package com.platform.modules.prj.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.platform.common.annotation.SysLog;
import com.platform.common.page.PageUtil;
import com.platform.common.utils.RestResponse;
import com.platform.modules.prj.dto.PrjProjectInfoByAppDto;
import com.platform.modules.prj.entity.PrjProjectEntity;
import com.platform.modules.prj.service.PrjProjectService;
import com.platform.modules.prj.vo.ProjectPersonnelVo;
import com.platform.modules.quality.vo.CommonResult;
import com.platform.modules.sys.controller.AbstractController;
import com.platform.modules.sys.entity.SysOrgEntity;
import com.platform.modules.sys.entity.SysUserEntity;
import com.platform.modules.sys.service.SysOrgService;
import com.platform.modules.sys.service.SysOrgUserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * Controller
 *
 * @author Zhouxw
 * @date 2019-06-20 17:17:28
 */
@Api(tags = "PrjProjectController|项目管理")
@RestController
@RequestMapping("prj/project")
public class PrjProjectController extends AbstractController {
    @Autowired
    private PrjProjectService prjProjectService;
    @Autowired
    private SysOrgService sysOrgService;
    @Autowired
    private SysOrgUserService sysOrgUserService;

    /**
     * 查看所有列表
     *
     * @param params 查询参数
     * @return RestResponse
     */
    /*@PostMapping("/queryAll")
    public RestResponse queryAll(@RequestParam Map<String, Object> params) {
        List<PrjProjectEntity> list = prjProjectService.queryAll(params);

        return RestResponse.success().put("list", list);
    }*/

    /**
     * 1、查询机构（建设、施工、监管等）参与的项目
     * @param prjName 查询参数
     * @return RestResponse
     */
    @ApiOperation(value = "查询项目", notes = "根据项目名称查询")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "currPage", value = "当前页数", dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "limit", value = "分页步数", dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "prjName", value = "项目名称", dataType = "String")
    })
    @PostMapping("/list")
    public RestResponse list(Integer currPage, Integer limit, @RequestParam(required = false) String prjName) {

        Map<String, Object> params = new HashMap<>();
        if (StringUtils.isNotBlank(prjName)) {
            params.put("prjName", prjName);
        }
        params.put("page", PageUtil.getCurrPage(currPage));
        params.put("limit", PageUtil.getLimit(limit));
        IPage<PrjProjectEntity> page = null;

        //获取机构
        SysOrgEntity org = getOrg();
        if("5".equals(org.getOrgType())) {
        	String xzqh = null;
        	if( 2 == org.getOrgLevel()) {
        		xzqh = org.getProvinceNo();
        	}else if(3==org.getOrgLevel()) {
        		xzqh = org.getCityNo();
        	}else if(4==org.getOrgLevel()) {
        		xzqh = org.getCountyNo();
        	}

        	//项目所属行政区划（省、市、区）
        	params.put("xzqh", xzqh);
        }else {
        	//项目所属企业机构（建设、施工、监理等）
        	params.put("orgNo", getOrgNo());
        }
        
        page = prjProjectService.queryPage(params);
        return RestResponse.success().put("page", page);
    }
    
    
    /**
     * 1、查询自己被分配的项目
     * @param prjName 查询参数
     * @return RestResponse
     */
    @ApiOperation(value = "查询被分配的项目", notes = "查询被分配的项目")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "currPage", value = "当前页数", dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "limit", value = "分页步数", dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "prjName", value = "项目名称", dataType = "String")
    })
    @PostMapping("/assignlist")
    public RestResponse assignlist(Integer currPage, Integer limit, @RequestParam(required = false) String prjName) {

    	
        Map<String, Object> params = new HashMap<>();
        if (StringUtils.isNotBlank(prjName)) {
            params.put("prjName", prjName);
        }
        params.put("userId", getUserId());
        params.put("page", PageUtil.getCurrPage(currPage));
        params.put("limit", PageUtil.getLimit(limit));
        Page page = prjProjectService.queryPrjSuperviseProjectPage(params);

        return RestResponse.success().put("page", page);
    }

    
    /**
     * 根据主键查询详情
     *
     * @param prjId 主键
     * @return RestResponse
     */
//    @ApiOperation(value = "查询一个项目", notes = "根据项目ID查询")
//    @ApiImplicitParams({
//            @ApiImplicitParam(paramType = "query", name = "prjId", value = "项目ID", dataType = "String", required = true)
//    })
//    @PostMapping("/info")
//    @Deprecated
//    public RestResponse info(@RequestParam String prjId) {
//        PrjProjectEntity prjProject = prjProjectService.getById(prjId);
//
//        return RestResponse.success().put("prjProject", prjProject);
//    }
    
    
    /**
     * 根据主键查询详情
     *
     * @param prjId 主键
     * @return RestResponse
     */
    @ApiOperation(value = "查询一个项目", notes = "根据项目ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "prjId", value = "项目ID", dataType = "String", required = true)
    })
    @GetMapping("/view")
    public CommonResult<PrjProjectEntity> view(@RequestParam String prjId) {
        PrjProjectEntity prjProject = prjProjectService.getById(prjId);
        return CommonResult.success(prjProject);
    }
    
    

    @ApiOperation(value = "项目创建获取当前登录者信息", notes = "项目创建获取当前登录者信息")
    @PostMapping("/buildOrgInfo")
    public RestResponse buildOrgInfo() {
        PrjProjectEntity prjProject = new PrjProjectEntity();

        //获取当前用户的企业信息
        SysOrgEntity org = sysOrgService.getById(getOrgNo());
        if ("1".equals(org.getOrgType())) {
            prjProject.setBuildCorpId(org.getOrgNo());
            prjProject.setBuildCorpName(org.getOrgName());
            prjProject.setBuildComartiperson(org.getComartiperson());
            return RestResponse.success().put("prjProject", prjProject);
        }
            
        return RestResponse.error("未找到该用户所属建设企业信息！");
    }

    @ApiOperation(value = "项目创建", notes = "项目创建")
    @SysLog("创建")
    @PostMapping("/save")
    public RestResponse save(PrjProjectEntity prjProject) {
        prjProject.setCreateTime(new Date());
        prjProject.setCreateUserId(getUserId());
        prjProjectService.add(prjProject);
        return RestResponse.success();
    }


    @ApiOperation(value = "项目修改", notes = "项目修改")
    @SysLog("修改")
    @PostMapping("/update")
    public RestResponse update(PrjProjectEntity prjProject) {
        prjProjectService.update(prjProject);
        return RestResponse.success();
    }

    /**
     * 根据主键删除
     *
     * @param prjId prjId
     * @return RestResponse
     */
    @ApiOperation(value = "项目删除", notes = "项目删除")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "prjId", value = "项目ID", dataType = "String", required = true)
    })
    @SysLog("删除")
    @PostMapping("/delete")
    public RestResponse delete(@RequestParam String prjId) {
        prjProjectService.delete(prjId);

        return RestResponse.success();
    }

    /**
     * 根据项目ID，联合备案内容，报审级别获取监管机构信息
     *
     * @param prjId prjId
     * @return RestResponse
     */
    @ApiOperation(value = "根据项目ID，联合备案内容，报审级别获取监管机构信息", notes = "根据项目ID，联合备案内容，报审级别获取监管机构信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "prjId", value = "项目ID", dataType = "String", required = true),
            @ApiImplicitParam(paramType = "query", name = "regulatorDeptType", value = "监管类型(zj;xf;rf)", dataType = "String", required = true),
            @ApiImplicitParam(paramType = "query", name = "regulatorDeptLevel", value = "报审级别", dataType = "String", required = true)
    })
    @PostMapping("/regulatorOrgInfo")
    public RestResponse regulatorOrgInfo(
            @RequestParam String prjId,
            @RequestParam String regulatorDeptType,
            @RequestParam String regulatorDeptLevel
    ) {
        PrjProjectEntity prjProject = prjProjectService.getById(prjId);



        return RestResponse.success();
    }
    

    /**
     * 根据项目ID查询项目详情
     *
     * @param prjId 项目ID
     * @return RestResponse
     */
    @ApiOperation(value = "根据项目ID查询项目详情", notes = "根据项目ID查询项目详情")
    @PostMapping("/infoByApp")
    public CommonResult<PrjProjectEntity> infoByApp(@RequestBody PrjProjectInfoByAppDto prjProjectInfoByAppDto) {
        PrjProjectEntity prjProject = prjProjectService.infoByApp(prjProjectInfoByAppDto.getPrjId());
        if(null==prjProject) {
            return CommonResult.error("项目不存在");
        }
        return CommonResult.success(prjProject);
    }
    
    /**
     * 根据项目ID查询项目详情
     *
     * @param prjId 项目ID
     * @return RestResponse
     */
    @ApiOperation(value = "根据项目ID查询项目详情", notes = "根据项目ID查询项目详情")
    @GetMapping("/info")
    public CommonResult<PrjProjectEntity> info(@RequestParam String prjId) {
        PrjProjectEntity prjProject = prjProjectService.info(prjId);
        if(null==prjProject) {
            return CommonResult.error("项目不存在");
        }
        return CommonResult.success(prjProject);
    }
    
    
    /**
     * 根据项目ID查询项目经理
     *
     * @param prjId 项目ID
     * @return CommonResult
     */
    @ApiOperation(value = "根据项目ID查询项目经理", notes = "根据项目ID查询项目经理")
    @PostMapping("/projectManagerByApp")
    public CommonResult<SysUserEntity> projectManagerByApp(@RequestBody PrjProjectEntity PrjProjectEntity) {
    	//不确定是否有多个项目经理的情况, 只返回第一个
    	List<SysUserEntity> users = prjProjectService.projectManagerByApp(PrjProjectEntity.getPrjId());
		if(null!=users && users.size()>0) {
			return CommonResult.success(users.get(0));
		}
		return CommonResult.error("未找到项目经理");
    }
    
    /**
     * 根据项目ID查询项目经理
     *
     * @param prjId 项目ID
     * @return CommonResult
     */
    @ApiOperation(value = "根据项目ID查询项目相关人员", notes = "根据项目ID查询项目相关人员")
    @PostMapping("/projectPersonnelByApp")
    public CommonResult<List<ProjectPersonnelVo>> projectPersonnelByApp(@RequestBody PrjProjectEntity PrjProjectEntity) {
    	List<ProjectPersonnelVo> projectPersonnelVo = prjProjectService.projectPersonnelByApp(PrjProjectEntity.getPrjId());
		return CommonResult.success(projectPersonnelVo);
    }

}
